package pri.jz.keeper.repository

import pri.jz.keeper.webservice.WebService
import pri.jz.keeper.webservice.retrofit.LoginService

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:
 */
class LoginRepository {
    private val loginService = WebService.retrofitClient.create(LoginService::class.java)
    companion object {
        const val LOGIN_SUCCESS: Int = 0xfffffff //登陆成功 用户id等于loginCode对此数异或
        const val LOGIN_FAIL_NON_EXIST = 102 //用户不存在
        const val LOGIN_FAIL_PASSWORD_WRONG = 103 //密码错误
    }

    /**
     * 用户登录
     * @param number 用户名
     * @param password 密码
     * @param onUserNonExist 用户不存在
     * @param onPasswordWrong 密码错误
     * @param onLoginSuccess 登陆成功
     */
    suspend fun userLogin(
        number: String,
        password: String,
        onUserNonExist: () -> Unit,
        onPasswordWrong: () -> Unit,
        onLoginSuccess: (Int) -> Unit
    ) {
        val loginCode = loginService.userLogin(number, password)
        when(loginCode) {
            LOGIN_FAIL_NON_EXIST -> onUserNonExist()
            LOGIN_FAIL_PASSWORD_WRONG -> onPasswordWrong()
            else -> onLoginSuccess(loginCode.xor(LOGIN_SUCCESS))
        }
    }


}